<?php

namespace Game\Controller;

use Common\Controller\AdminAuthController;

class ListController extends CstBaseController
{
    private $roleLogic;
    const TABLE_NAME="List";

    public function _initialize()
    {
        parent::_initialize();
        $this->roleLogic = A('Console/Role', 'Logic');
    }

    public function index() {
        
        $title=trim(I('get.title','','trim'));
        $type=trim(I('get.type',0,'intval'));
        if(!empty($title)){
            $map["title"]=array('like','%'.$title.'%');
        }
        $map["type"]=$type;
        $list=D(static::TABLE_NAME)->where($map)->order('sort DESC')->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function edit() {
        $id = I('get.id', 0, intval);
        $activity_info = D(static::TABLE_NAME)->where('id='.$id)->find();

        $this->assign('info', $activity_info);
        $this->display();
    }
    public function doAdd() {
        $banner_model = D(static::TABLE_NAME);
        $data = I('post.','','trim');
        
        $userInfo = $this->getUserInfo();
        $data["user_id"]= $userInfo['id'];
        $data["add_time"]= date('Y-m-d H:i:s');
        $result = $banner_model->add($data);
        
        if ($result) {
            
            exit(successInfoFoxJson('新增成功！'));
        } else {
            exit(errorInfoFoxJson('新增失败！'));
        }
    }
    public function doEdit() {
        $model = D(static::TABLE_NAME);
        $data = I('post.','','trim');
        
        $userInfo = $this->getUserInfo();
        $data["update_user"]= $userInfo['id'];
        $data["update_time"]= date('Y-m-d H:i:s');
        $result = $model->where(array('id'=>$data['id']))->save($data);
        
        if ($result) {
            exit(successInfoFoxJson('修改成功！'));
        } else {
            exit(errorInfoFoxJson('修改失败！'));
        }
    }

    public function doDelete() {
        $ids = I('get.id', 0, 'intval');
        if (empty($ids)) {
            $id = I('post.id');
            $ids = implode(',', $id);
            $map['id'] = array('IN', $ids);
        } else {
            $map['id'] = $ids;
        }
        $map["state"]=$ids = I('get.state', 0, 'intval');
        $successValue='删除成功';
        $errorVaule="删除失败";
        if($map['state']==1){
            $successValue='恢复成功';
            $errorVaule="恢复失败";
        }
        $model = D(static::TABLE_NAME);
        $userInfo = $this->getUserInfo();
        $map["update_user"]= $userInfo['id'];
        $map["update_time"]= date('Y-m-d H:i:s');
        $result = $model->save($map);
        if ($result) {
            exit(successInfoFoxJson($successValue));
        } else {
            exit(errorInfoFoxJson($errorVaule));
        }
    }
}